package acs.bridge;

import acs.bridge.dao.bm_load_way4DAOImpl;
import acs.bridge.model.bm_load_ch_way4_model;
import org.apache.log4j.Logger;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Map;

/**
 * acs.bridge
 * User: KuznetsovStV
 * Date: 17.04.14
 * Time: 10:12
 */
public class bm_load_ch_way4_handler extends DefaultHandler {
    Logger logger = Logger.getLogger(this.getClass().getName());
    Integer g_count = 0;

    private String check_contract_number = "1";

    boolean bdoc = false;
    boolean bdocrefset = false;
    boolean bdocrefset_parm = false;
    boolean bbdocrefset_parmcode = false;


    boolean bterm = false;
    boolean bcardnumber = false;
    boolean bdestionation = false;
    boolean blocaldt = false;
    boolean bextra = false;
    boolean bextra_type = false;

    boolean bextra_adddata = false;
    boolean btransaction = false;
    boolean badddata_parm = false;
    boolean badddata_parmcode = false;
    boolean badddata_parmvalue = false;
    boolean btrans_type = false;
    boolean btrans_code = false;
    boolean brequest_category = false;
    boolean boriginator = false;
    boolean btransitcontractnumber = false;
    boolean bchannel = false;
    boolean bproduct = false;
    boolean bamount = false;

    public void setBm_load_way4DAOImpl(acs.bridge.dao.bm_load_way4DAOImpl bm_load_way4DAOImpl) {
        this.bm_load_way4DAOImpl = bm_load_way4DAOImpl;
        setContractNumber((ArrayList<Map>) this.bm_load_way4DAOImpl.getContractNumberList());
    }


    public ArrayList<Map> getContractNumber() {
        return contractNumber;
    }


    public void setContractNumber(ArrayList<Map> contractNumber) {
        this.contractNumber = contractNumber;
    }

    private ArrayList<Map> contractNumber;


    acs.bridge.dao.bm_load_way4DAOImpl bm_load_way4DAOImpl;


    String cur_docrefset;
    String cur_extratype;
    String cur_adddatatype;
    boolean bbdocrefset_parmvalue = false;

    bm_load_ch_way4_model l_bm_load_ch_way4_model;

    public void startElement(String uri, String localName, String qName,
                             Attributes attributes) throws SAXException {

        //   System.out.println("Start Element :" + qName);

        if (qName.equalsIgnoreCase("DOC")) {
            bdoc = true;
            l_bm_load_ch_way4_model = new bm_load_ch_way4_model();
        }

        if (bdoc && qName.equalsIgnoreCase("DESTINATION")) {
            bdestionation = true;
        }


        if (bdoc && qName.equalsIgnoreCase("TRANSTYPE")) {
            btrans_type = true;
        }


        if (bdoc && qName.equalsIgnoreCase("ORIGINATOR")) {
            boriginator = true;
        }


        if (bdoc && boriginator && qName.equalsIgnoreCase("TRANSITCONTRACTNUMBER")) {
            btransitcontractnumber = true;
        }


        if (bdoc && btrans_type && qName.equalsIgnoreCase("TRANSCODE")) {
            btrans_code = true;
        }


        if (bdoc && btrans_type && btrans_code && qName.equalsIgnoreCase("REQUESTCATEGORY")) {
            brequest_category = true;
        }


        if (bdoc && qName.equalsIgnoreCase("LOCALDT")) {
            blocaldt = true;
        }


        if (bdoc && qName.equalsIgnoreCase("TRANSACTION")) {
            btransaction = true;
        }

        if (btransaction && qName.equalsIgnoreCase("EXTRA")) {
            bextra = true;
        }


        if (btransaction && bextra && qName.equalsIgnoreCase("TYPE")) {
            bextra_type = true;
        }

        if (btransaction && qName.equalsIgnoreCase("AMOUNT")) {
            bamount = true;
        }


        if (btransaction && bextra && qName.equalsIgnoreCase("ADDDATA") && cur_extratype.equals("AddInfo")) {
            bextra_adddata = true;
        }


        if (boriginator && qName.equalsIgnoreCase("CONTRACTNUMBER")) {
            bterm = true;
        }

        if (bdestionation && qName.equalsIgnoreCase("CONTRACTNUMBER")) {
            bcardnumber = true;
        }


        if (qName.equalsIgnoreCase("DOCREFSET")) {
            bdocrefset = true;
        }

        if (bdocrefset && qName.equalsIgnoreCase("PARM")) {
            bdocrefset_parm = true;
        }

        if (bdocrefset_parm && qName.equalsIgnoreCase("PARMCODE")) {
            bbdocrefset_parmcode = true;
        }
        if (bdocrefset_parm && qName.equalsIgnoreCase("VALUE")) {
            bbdocrefset_parmvalue = true;
        }
        if (bextra_adddata && qName.equalsIgnoreCase("PARM")) {
            badddata_parm = true;
        }


        if (bextra_adddata && badddata_parm && qName.equalsIgnoreCase("PARMCODE")) {
            badddata_parmcode = true;
        }

        if (bextra_adddata && badddata_parm && qName.equalsIgnoreCase("VALUE")) {
            badddata_parmvalue = true;
        }


        if (bdestionation && qName.equalsIgnoreCase("PRODUCT")) {
            bproduct = true;
        }


        if (bproduct && qName.equalsIgnoreCase("CHANNEL")) {
            bchannel = true;
        }

    }

    public void endElement(String uri, String localName,
                           String qName) throws SAXException {

        if (qName.equalsIgnoreCase("DOC")) {
            bdoc = false;


            boolean l_for_this_npo = false;

            if (check_contract_number.equals("0")) {
                l_for_this_npo = true;
            } else {
                for (Map row : getContractNumber()) {
                    if (row.get("CONTRACT_NUMBER").equals(l_bm_load_ch_way4_model.getTransitcontractnumber()))
                        l_for_this_npo = true;
                }
            }
            if (l_for_this_npo) {
                bm_load_way4DAOImpl.insertRawFile_ch(l_bm_load_ch_way4_model);
                l_bm_load_ch_way4_model.setNull();
                if ((g_count % 1000) == 0) {
                    logger.info("count=" + g_count);
                    try {
                        bm_load_way4DAOImpl.getJdbcTemplate().getDataSource().getConnection().commit();
                    } catch (SQLException e) {
                        logger.error(e.getStackTrace());
                    }
                }
                g_count = g_count + 1;

            }
            l_bm_load_ch_way4_model = null;
        }

        if (bdoc && qName.equalsIgnoreCase("LOCALDT")) {
            blocaldt = false;
        }

        if (bdoc && qName.equalsIgnoreCase("DESTINATION")) {
            bdestionation = false;
        }


        if (bdoc && qName.equalsIgnoreCase("ORIGINATOR")) {
            boriginator = false;
        }


        if (bdoc && boriginator && qName.equalsIgnoreCase("TRANSITCONTRACTNUMBER")) {
            btransitcontractnumber = false;
        }


        if (bdoc && qName.equalsIgnoreCase("DESTINATION")) {
            bdestionation = false;
        }
        if (bdoc && qName.equalsIgnoreCase("TRANSACTION")) {
            btransaction = false;
        }


        if (boriginator && qName.equalsIgnoreCase("CONTRACTNUMBER")) {
            bterm = false;
        }
        if (bdestionation && qName.equalsIgnoreCase("CONTRACTNUMBER")) {
            bcardnumber = false;
        }


        if (qName.equalsIgnoreCase("DOCREFSET")) {
            bdocrefset = false;

        }
        if (bdocrefset && qName.equalsIgnoreCase("PARM")) {
            bdocrefset_parm = false;
            cur_docrefset = null;
        }

        if (bdocrefset_parm && qName.equalsIgnoreCase("PARMCODE")) {
            bbdocrefset_parmcode = false;

        }

        if (bdocrefset_parm && qName.equalsIgnoreCase("VALUE")) {
            bbdocrefset_parmvalue = false;
        }


        if (btransaction && bextra && qName.equalsIgnoreCase("TYPE")) {
            bextra_type = false;

        }


        if (btransaction && qName.equalsIgnoreCase("EXTRA")) {
            bextra = false;
            cur_extratype = null;
        }


        if (btransaction && qName.equalsIgnoreCase("AMOUNT")) {
            bamount = false;
        }

        if (btransaction && bextra && qName.equalsIgnoreCase("ADDDATA") && cur_extratype.equals("AddInfo")) {
            bextra_adddata = false;
        }

        if (bextra_adddata && qName.equalsIgnoreCase("PARM")) {
            badddata_parm = false;
        }

        if (bextra_adddata && badddata_parm && qName.equalsIgnoreCase("PARMCODE")) {
            badddata_parmcode = false;

        }

        if (bextra_adddata && badddata_parm && qName.equalsIgnoreCase("VALUE")) {
            badddata_parmvalue = false;
            cur_adddatatype = null;
        }


        if (bdoc && qName.equalsIgnoreCase("TRANSTYPE")) {
            btrans_type = false;
        }

        if (bdoc && btrans_type && qName.equalsIgnoreCase("TRANSCODE")) {
            btrans_code = false;
        }

        if (bdoc && btrans_type && btrans_code && qName.equalsIgnoreCase("REQUESTCATEGORY")) {
            brequest_category = false;
        }


        if (bdestionation && qName.equalsIgnoreCase("PRODUCT")) {
            bproduct = false;
        }

        if (bproduct && qName.equalsIgnoreCase("CHANNEL")) {
            bchannel = false;
        }

    }

    public void characters(char ch[], int start, int length) throws SAXException {

        if (bbdocrefset_parmcode) {
            if (cur_docrefset == null)
                cur_docrefset = new String(ch, start, length);
            else
                cur_docrefset = cur_docrefset + new String(ch, start, length);
        }

        if (bbdocrefset_parmvalue && cur_docrefset != null) {
            if (cur_docrefset.equals("DRN")) {
                l_bm_load_ch_way4_model.setDrn(new String(ch, start, length));
            }
        }


        if (bbdocrefset_parmvalue && cur_docrefset != null) {
            if (cur_docrefset.equals("RRN")) {
                l_bm_load_ch_way4_model.setRrn(new String(ch, start, length));
            }
        }


        if (bbdocrefset_parmvalue && cur_docrefset != null) {
            if (cur_docrefset.equals("AuthCode")) {
                l_bm_load_ch_way4_model.setAuthcode(new String(ch, start, length));
            }
        }

        if (bbdocrefset_parmvalue && cur_docrefset != null) {
            if (cur_docrefset.equals("ONLINE")) {
                l_bm_load_ch_way4_model.setPm_online(new String(ch, start, length));
            }
        }

        if (blocaldt) {
            l_bm_load_ch_way4_model.setLocaldt((new String(ch, start, length)));

        }

        if (bterm) {
            l_bm_load_ch_way4_model.setContractnumber(new String(ch, start, length));
        }

        if (bamount) {
            l_bm_load_ch_way4_model.setAmount(new String(ch, start, length));
        }


        if (bextra_type) {
            if (cur_extratype == null)
                cur_extratype = new String(ch, start, length);
            else {
                cur_extratype = cur_extratype + new String(ch, start, length);
            }
        }

        if (btransitcontractnumber) {
            l_bm_load_ch_way4_model.setTransitcontractnumber(new String(ch, start, length));
        }

        if (badddata_parmcode) {
            if (cur_adddatatype == null)
                cur_adddatatype = new String(ch, start, length);
            else
                cur_adddatatype = cur_adddatatype + new String(ch, start, length);
        }

        if (brequest_category) {
            l_bm_load_ch_way4_model.setRequestcategory(new String(ch, start, length));
        }

        if (bchannel) {
            l_bm_load_ch_way4_model.setChannel(new String(ch, start, length));
        }
    }

    public String getCheck_contract_number() {
        return check_contract_number;
    }

    public void setCheck_contract_number(String check_contract_number) {
        this.check_contract_number = check_contract_number;
    }
}
